//
//  FormalLoanViewController.m
//  LoanOne
//
//  Created by WangZhenyu on 2017/10/9.
//  Copyright © 2017年 WangZhenyu. All rights reserved.
//

#import "FormalLoanViewController.h"
#import "FormalLoanTableViewCell.h"
#import "MJRefresh.h"
#import "NoContentView.h"
#import "ProductItem.h"
#import "WebShowViewController.h"

@interface FormalLoanViewController (){
    NSMutableArray                 *contentMutArr;
    NSInteger                      pageNum;
}

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation FormalLoanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    pageNum = 0;
    contentMutArr = [NSMutableArray array];
    
    //集成上下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeader)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooter)];
    
    
    [self requestContent:YES];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshHeader{
    pageNum = 0;
    [self requestContent:NO];
}

- (void)refreshFooter{
    pageNum = ++pageNum;
    [self requestContent:NO];
}

//请求数据
- (void)requestContent:(BOOL)alert{
    if (alert) {
        [self loadingHUDAlert:@"正在加载"];
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:[NSNumber numberWithLong:[self.categoryId longLongValue]] forKey:@"categoryId"];
    [dic setObject:[NSNumber numberWithInteger:pageNum] forKey:@"page"];
    [dic setObject:[NSNumber numberWithInteger:20] forKey:@"size"];
    
    [[ServiceRequest sharedService] GET:@"products" parameters:dic success:^(id responseObject) {
        [self hideHudAlert];
        if (pageNum == 0) {
            [self.tableView.mj_header endRefreshing];
            [contentMutArr removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        NSArray *arr = responseObject;
        for (int i = 0; i < arr.count; i++) {
            [contentMutArr addObject:[ProductItem productItemDic:[arr objectAtIndex:i]]];
        }
        if (contentMutArr.count == 0) {
            self.tableView.tableFooterView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_Default title:@"暂无贷款产品"];
        }else{
            self.tableView.tableFooterView = nil;
        }
        if (pageNum > 0 && arr.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^(NSString *error, NSInteger code) {
        [self hideHudAlert];
        if (pageNum == 0) {
            [self.tableView.mj_header endRefreshing];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        if (code == 0) {
            self.tableView.tableFooterView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_NoNetWork title:error];
        }else if (code == 404){
            if (pageNum == 0) {
                [contentMutArr removeAllObjects];
                self.tableView.tableFooterView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_Default title:@"暂无贷款产品"];
                [self.tableView reloadData];
            }
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self showHUDAlert:error];
        }
    }];
    
}


#pragma mark - UITableView datasource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return contentMutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"FormalLoanCell";
    FormalLoanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"FormalLoanTableViewCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setContent:[contentMutArr objectAtIndex:indexPath.row]];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    ProductItem *item = [contentMutArr objectAtIndex:indexPath.row];
    WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
    viewCon.urlStr = item.url;
    [self.navigationController pushViewController:viewCon animated:YES];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setObject:[NSNumber numberWithInteger:[item.productId integerValue]] forKey:@"productId"];
        [dic setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] forKey:@"appName"];
        [[ServiceRequest sharedService] POST:@"trigger/product" parameters:dic success:^(id responseObject) {
            NSLog(@"success");
        } failure:^(NSString *error, NSInteger code) {
            
        }];
    });
}

@end
